/**
 * 不正常事件清单
 */
<template>
	<vp-main-page @refresh="loadDataList" :tableRef="tableRef">
		<template #search>
			<el-form ref="searchFormRef" :model="searchForm" @submit.prevent @keyup.enter="loadDataList" label-width="120px">
				<vp-search-row :show-more="showMore">
					<vp-search-col>
						<el-form-item prop="keyword">
							<template #label><vp-label zh="关键字" en="Keyword"/></template>
							<el-input v-model="searchForm.keyword" v-trim></el-input>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="fjjxList">
							<template #label><vp-label zh="机型" en="A/C Type"/></template>
							<VpAircraftTypeMultSelect
								:selected="searchForm.fjjxList"
								@aircraftModelMultSelected="(data: any) => {searchForm.fjjxList = data}"
								class="w100"
							/>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="fjzchList">
							<template #label><vp-label zh="机号" en="A/C REG"/></template>
							<AircraftMultSelect 
								:selected="searchForm.fjzchList" 
								:fjjxList="searchForm.fjjxList"
								@aircraftMultSelected="(data: any) => {searchForm.fjzchList = data}"
								class="w100"
							/>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="sjrq">
							<template #label><vp-label zh="事件日期" en="Incident Date"/></template>
							<el-date-picker
								v-model="searchForm.sjrq"
								type="daterange"
								start-placeholder="开始日期/Start Date"
								end-placeholder="结束日期/End Date"
								value-format="YYYY-MM-DD"
								unlink-panels
								range-separator="-"
								class="w100"
							>
							</el-date-picker>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="sfdmc">
							<template #label><vp-label zh="事发地点" en="Incident Location"/></template>
							<el-input v-model="searchForm.sfdmc" v-trim></el-input>
						</el-form-item>
					</vp-search-col>
					<vp-search-col btn-col>
						<vp-btn-search :loading="loading" @click="loadDataList"/>
						<vp-btn-reset @click="handleSearchReset"/>
						<vp-btn-more @change="showMore = $event"/>
					</vp-search-col>
				</vp-search-row>
			</el-form>
		</template>
		<template #actions>
			<vp-btn-add v-auth="'qualitySafetyAbnormalList.add'" @click="handleAdd"/>
			<vp-btn-export @click="handleExport"/>
		</template>
		<template #table>
			<vp-vxe-table
				id="qualitySafetyAbnormalListTable"
				ref="tableRef" 
				:data="tableData" 
				@sort-change="handleSortChange" 
				row-key="id"
				v-loading="loading"
			>	
				<vxe-column type="seq" align="center" title="#" width="50" />
				<vxe-column 
					sortable
					field="bh" 
					title="编号 No."
					show-overflow
					width="110"
				>
					<template #header><vp-label zh="编号" en="No."/></template>
					<template #default="scope">
						<span v-html="markTextColor(searchForm.keyword, scope.row.bh)"/>
					</template>
				</vxe-column>
				<vxe-column 
					sortable
					field="sjrq" 
					title="事件日期 Incident Date"
					show-overflow
					width="130"
				>
					<template #header><vp-label zh="事件日期" en="Incident Date"/></template>
				</vxe-column>
				<vxe-column 
					sortable
					field="fjjx" 
					title="机型 A/C Type"
					show-overflow 
					width="110"
				>
					<template #header><vp-label zh="机型" en="A/C Type"/></template>
				</vxe-column>
				<vxe-column 
					sortable
					field="fjzch" 
					title="机号 A/C REG"
					show-overflow 
					width="100"
				>
					<template #header><vp-label zh="机号" en="A/C REG"/></template>
				</vxe-column>
				<vxe-column 
					sortable
					field="sfdmc" 
					title="事发地点 Incident Loc."
					show-overflow 
					width="130"
				>
					<template #header><vp-label zh="事发地点" en="Incident Loc."/></template>
				</vxe-column>
				<vxe-column 
					sortable
					field="sjjg" 
					title="事件经过 Incident Process"
					show-overflow 
					width="200"
				>
					<template #header><vp-label zh="事件经过" en="Incident Process"/></template>
					<template #default="scope">
						<span v-html="markTextColor(searchForm.keyword, scope.row.sjjg)"/>
					</template>
				</vxe-column>
				<vxe-column 
					sortable
					field="clff" 
					title="处理方法 Handling Method"
					show-overflow 
					width="200"
				>
					<template #header><vp-label zh="处理方法" en="Handling Method"/></template>
					<template #default="scope">
						<span v-html="markTextColor(searchForm.keyword, scope.row.clff)"/>
					</template>
				</vxe-column>
				<vxe-column 
					sortable
					field="fsjd" 
					title="发生阶段 Occur. Stage"
					show-overflow 
					width="130"
				>
					<template #header><vp-label zh="发生阶段" en="Occur. Stage"/></template>
				</vxe-column>
				<vxe-column 
					sortable
					field="sjzt" 
					title="状态 Status"
					show-overflow 
					width="90"
				>
					<template #header><vp-label zh="状态" en="Status"/></template>
				</vxe-column>
				<vxe-column 
					sortable
					field="sjxz" 
					title="事件性质 Nature"
					show-overflow 
					width="110"
				>
					<template #header><vp-label zh="事件性质" en="Nature"/></template>
				</vxe-column>
				<vxe-column 
					sortable
					field="bsjufbs" 
					title="报送局方 Submit To CAAC"
					width="150"
				>
					<template #header><vp-label zh="报送局方" en="Submit To CAAC"/></template>
					<template #default="scope">
						<el-tag v-if="scope.row.bsjufbs === 1" type="danger" effect="dark" style="width: 80px;">Y</el-tag>
						<el-tag v-if="scope.row.bsjufbs === 0" type="success" effect="dark" style="width: 80px;">N</el-tag>
					</template>
				</vxe-column>
				<vxe-column 
					sortable
					field="bsjiafbs" 
					title="报送甲方 Submit To Party A"
					width="160"
				>
					<template #header><vp-label zh="报送甲方" en="Submit To Party A"/></template>
					<template #default="scope">
						<el-tag v-if="scope.row.bsjiafbs === 1" type="danger" effect="dark" style="width: 80px;">Y</el-tag>
						<el-tag v-if="scope.row.bsjiafbs === 0" type="success" effect="dark" style="width: 80px;">N</el-tag>
					</template>
				</vxe-column>
				<vxe-column 
					sortable
					field="whrmc" 
					title="维护人 Editor"
					show-overflow 
					width="100"
				>
					<template #header><vp-label zh="维护人" en="Editor"/></template>
				</vxe-column>
				<vxe-column 
					sortable
					field="whsj" 
					title="维护时间 Edit Time"
					show-overflow  
					width="160"
				>
					<template #header><vp-label zh="维护时间" en="Edit Time"/></template>
				</vxe-column>
				<vxe-column 
					field="operation" 
				 	title="操作 Operations"
					fixed="right" 
					width="140"
				>
					<template #header><vp-label-operations/></template>
					<template #default="scope">
						<el-space>
							<vp-btn-edit text v-auth="'qualitySafetyAbnormalList.edit'" @click="handleEdit(scope.row)"/>
							<vp-btn-del text v-auth="'qualitySafetyAbnormalList.edit'" @click="handleDelete(scope.row)"/>
							<vp-btn-view text @click="handleView(scope.row.id)"/>
						</el-space>
					</template>
				</vxe-column>
			</vp-vxe-table>
		</template>
		<template #pagination>
			<vp-pagination
				@change="loadDataList"
				:total="page.total" 
				v-model:currentPage="page.pageNum"
				v-model:pageSize="page.pageSize"
			>
			</vp-pagination>
		</template>
		<EditAbnormalList ref="editAbnormalListRef" @formSubmited="handleFormSubmited"></EditAbnormalList>
	</vp-main-page>
</template>
<script setup lang="ts" name="qualitySafetyAbnormalList">
import { onMounted, reactive, ref } from 'vue'
import request from '/@/utils/request';
import EditAbnormalList from './component/EditAbnormalList.vue';
import { markTextColor } from '/@/utils/toolsValidate';
import { useRouter } from 'vue-router';
import AircraftMultSelect from '/@/components/vp/aircraft/VpAircraftMultSelect.vue';
import VpAircraftTypeMultSelect from "/@/components/vp/aircraftType/VpAircraftTypeMultSelect.vue";
import { DEFAULT_PAGE } from '/@/utils/page';
import VpMessageBox from '/@/components/vp/VpMessageBox';
import commonFunction from '/@/utils/commonFunction';

const searchForm = reactive({
	keyword: "",
	fjjxList: [],
	fjzchList: [],
	fsjd: [],
	gzhg: [],
	sfrq: [],
	sfdd: "",
	bgrq: [],
	ztList: [10, 88, 89],
});

const page = reactive({
	...DEFAULT_PAGE,
})

const showMore = ref(false);
const loading = ref(false);
const tableData = ref([]);

const router = useRouter();

// 加载列表数据
const loadDataList = () => {
	loading.value = true
	request.get('/abnormal/list/list', {
		params: {
			...searchForm,
			...page,
		}
	}).then(res => {
		tableData.value = res?.data?.list || [];
		page.total = res?.data?.total || 0;
		page.pageNum = res?.data?.pageNum || 1;
	}).finally(()=>{
		loading.value = false;
	})
}

const searchFormRef = ref();
// 重置搜索条件
const handleSearchReset = ()=>{
	searchFormRef.value.resetFields();
}

// 列表字段排序
const handleSortChange = (column: any)=>{
	page.sortColumn = column.field;
	page.sortOrder = column.order;
	loadDataList()
}

onMounted(() => {
	loadDataList();
})

const editAbnormalListRef = ref();
const tableRef = ref();

const handleAdd = ()=>{
	editAbnormalListRef.value.handleOpen({
		type: 'add'
	});
}

const handleEdit = (row: any)=>{
	editAbnormalListRef.value.handleOpen({
		type: 'edit',
		id: row.id
	});
}

const handleFormSubmited = ()=>{
	loadDataList();
}

const handleView = (id: string)=>{
	router.push({
		name: "viewAbnormalList",
		params: {
			id: id,
		},
	})
}

// 删除
const handleDelete = (row: any)=>{
	VpMessageBox.confirmDelete().then(()=>{
		loading.value = true;
		request.delete(`/abnormal/list/${row.id}`).then(res => {
			loadDataList();
		}).catch(() => {}).finally(() => {
			loading.value = false;
		})
	});
}

const {exportFile} = commonFunction();

const handleExport = () =>{

	exportFile({
		url: `/abnormal/list/export`, 
		params: {
			...searchForm,
			cnbs : 1,
		},
		fileName: '不正常事件清单.xlsx',
	});
}
</script>